
*For Margaret dropbox folders 
global NCREIF "/Users/BeckaBrolinson/Dropbox/NCREIF/data" 
global build 	"$NCREIF/build" 
global analysis "$NCREIF/analysis"
global results 	"$analysis/results"
global figures 	"$analysis/figures" 


*------------------------------------------------------------------------------*
*	Step 9- Matched Event Study	Robustness with 3 years before period dropped    *
*------------------------------------------------------------------------------*	

	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 



*Event Study 
	set more off 
	forvalues j=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`j'_wmatches.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
	
	*Generate Variable for Intensity of Treatment 
	bysort propnum: gen rate=everrated if year>=firstyearrated
	*Generate different bins for rating 	
	gen rate1=1 if rate>=75 & rate<82 
	replace rate1=0 if missing(rate1)
	gen rate2=1 if rate>=82 & rate<91 
	replace rate2=0 if missing(rate2)
	gen rate3=1 if rate>=91 & rate<100 
	replace rate3=0 if missing(rate3)
	*Generate a catogory for these so that I can plot them 
	gen rate_cat=. 
	replace rate_cat=1 if rate1==1 
	replace rate_cat=2 if rate2==1 
	replace rate_cat=3 if rate3==1 
	
	*Generate time since Treatment 
	gen years_pre_post_cert= year-firstyearrated
	*Generate indicators for 15 years pre and post cert (30 indicators)
	tostring years_pre_post_cert, replace
	replace years_pre_post_cert=subinstr(years_pre_post_cert, "-", "_", 1 )
	local years "_15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	foreach year of local years{
	generate pre`year'=1 if years_pre_post_cert=="`year'"
	}
	
	*Generate the interaction variable: 
	local years "_15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	foreach year of local years{
	generate interaction`year'=treat*pre`year'
	replace interaction`year'=0 if missing(interaction`year')
	label variable interaction`year' "`year'"
	}
	*Generate the interaction variable with the intensity var : 
	local years "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	local intensity "1 2 3" 
	foreach year of local years{
	foreach i of local intensity{
	generate interaction`i'_`year'=rate`i'*pre`year'
	replace interaction`i'_`year'=0 if missing(interaction`i'_`year')
	label variable interaction`i'_`year' "Rating Bin `i' `year'"
	}
	}	

	#d ;
	global interaction "interaction_15 interaction_14 interaction_13 interaction_12
	interaction_11 interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	interaction_2 interaction_1 interaction0
	interaction1 interaction2 interaction3 interaction4 
	interaction5 interaction6 interaction7 interaction8 
	interaction9 interaction10 interaction11 interaction12
	interaction13 interaction14 interaction15";
	
	global interaction10 "interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	interaction_2 interaction_1 interaction0
	interaction1 interaction2 interaction3 interaction4 
	interaction5 interaction6 interaction7 interaction8 
	interaction9 interaction10";
	
	
	global rate1_interaction "interaction_15 interaction_14 interaction_13 interaction_12
	interaction_11 interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	 interaction_2 interaction_1 interaction1_0
	interaction1_1 interaction1_2 interaction1_3 interaction1_4 
	interaction1_5 interaction1_6 interaction1_7 interaction1_8 
	interaction1_9 interaction1_10 interaction1_11 interaction1_12
	interaction1_13 interaction1_14 interaction1_15";
	
	global rate2_interaction "interaction2_0 interaction2_1 interaction2_2 interaction2_3 interaction2_4 
	interaction2_5 interaction2_6 interaction2_7 interaction2_8 
	interaction2_9 interaction2_10 interaction2_11 interaction2_12
	interaction2_13 interaction2_14 interaction2_15";
	
	global rate3_interaction "interaction3_0 interaction3_1 interaction3_2 interaction3_3 interaction3_4 
	interaction3_5 interaction3_6 interaction3_7 interaction3_8 
	interaction3_9 interaction3_10 interaction3_11 interaction3_12
	interaction3_13 interaction3_14 interaction3_15";
	#d cr 
	
	*Fe regression of util Exp with Heteroscedasticity and autocorrelation-consistent asymptotic variance (HAC)
	preserve
	eststo MatchedUtilEventStudyCov1: parmby "reghdfe logrealutilpersf $Cov1 rl_yr_rentpersf $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	*findit eclplot 
	*Plot the point estimates 
	keep if parmseq>=17 & parmseq<=30
	
	*Add row to plot the 0 point estimate 
	set obs `=_N+1'
	replace label = "_3" if label == "" 
	replace parmseq = 20.5 if parmseq == . 
	replace estimate = 0 if estimate == . 
	sort parmseq
	
	*Change the _ to - in the label variable 
	replace label=subinstr(label, "_", "-", 1 )
	sencode label if parm!="_cons", gene(parmlab)

	*Plot the outcomes 
	eclplot estimate min95 max95 parmlab, ytitle("Coefficient Estimates") ///
	xtitle("Treatment Effect By Years Before/After Certification") yline(0, lcolor(navy*.5)) ///
	xlabel(#15) bgcolor(white) graphregion(color(white)) ylab(,nogrid) xlab(,nogrid) legend(on ring(1) position(6) lab(1 "95% CI"))
	gr export "$figures/19_util_eventstudy_matched`j'_3dropped.png", replace	
	restore
	
	preserve
	eststo MatchedUtilEventStudyCov2: parmby "reghdfe logrealutilpersf $Cov2 rl_yr_rentpersf $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	restore 
	preserve 
	eststo MatchedUtilEventStudyCov3: parmby "reghdfe logrealutilpersf $Cov3 rl_yr_rentpersf $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	restore
	
	*Fe regression with Heteroscedasticity and autocorrelation-consistent asymptotic variance (HAC)
	preserve
	eststo MatchedRentEventStudyCov1: parmby "reghdfe logrealrentpersf $Cov1 $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	*findit eclplot 
	*Plot the point estimates 
	keep if parmseq>=16 & parmseq<=29
	
	*Add row to plot the 0 point estimate 
	set obs `=_N+1'
	replace label = "_3" if label == "" 
	replace parmseq = 19.5 if parmseq == . 
	replace estimate = 0 if estimate == . 
	sort parmseq
	
	*Change the _ to - in the label variable 
	replace label=subinstr(label, "_", "-", 1 )
	sencode label if parm!="_cons", gene(parmlab)

	*Plot the outcomes 
	eclplot estimate min95 max95 parmlab, ytitle("Coefficient Estimates") ///
	xtitle("Treatment Effect By Years Before/After Certification") yline(0, lcolor(navy*.5)) ///
	xlabel(#15) bgcolor(white) graphregion(color(white)) ylab(,nogrid) xlab(,nogrid) legend(on ring(1) position(6) lab(1 "95% CI"))
	gr export "$figures/19_rent_eventstudy_matched`j'_3dropped.png", replace
	restore	
	
	preserve
	eststo MatchedRentEventStudyCov2: parmby "reghdfe logrealrentpersf $Cov2 $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	restore 
	preserve 
	eststo MatchedRentEventStudyCov3: parmby "reghdfe logrealrentpersf $Cov3 $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	restore
	
	
	}
	
	
	**Repeat regressions to put output into latex table
		
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch2_wmatches.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	

	*Generate Variable for Intensity of Treatment 
	bysort propnum: gen rate=everrated if year>=firstyearrated
	*Generate different bins for rating 	
	gen rate1=1 if rate>=75 & rate<82 
	replace rate1=0 if missing(rate1)
	gen rate2=1 if rate>=82 & rate<91 
	replace rate2=0 if missing(rate2)
	gen rate3=1 if rate>=91 & rate<100 
	replace rate3=0 if missing(rate3)
	*Generate a catogory for these so that I can plot them 
	gen rate_cat=. 
	replace rate_cat=1 if rate1==1 
	replace rate_cat=2 if rate2==1 
	replace rate_cat=3 if rate3==1 
	
	*Generate time since Treatment 
	gen years_pre_post_cert= year-firstyearrated
	*Generate indicators for 15 years pre and post cert (30 indicators)
	tostring years_pre_post_cert, replace
	replace years_pre_post_cert=subinstr(years_pre_post_cert, "-", "_", 1 )
	local years "_15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	foreach year of local years{
	generate pre`year'=1 if years_pre_post_cert=="`year'"
	}
	
	*Generate the interaction variable: 
	local years "_15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	foreach year of local years{
	generate interaction`year'=treat*pre`year'
	replace interaction`year'=0 if missing(interaction`year')
	label variable interaction`year' "`year'"
	}
	*Generate the interaction variable with the intensity var : 
	local years "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	local intensity "1 2 3" 
	foreach year of local years{
	foreach i of local intensity{
	generate interaction`i'_`year'=rate`i'*pre`year'
	replace interaction`i'_`year'=0 if missing(interaction`i'_`year')
	label variable interaction`i'_`year' "Rating Bin `i' `year'"
	}
	}	

	#d ;
	global interaction "interaction_15 interaction_14 interaction_13 interaction_12
	interaction_11 interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	interaction_2 interaction_1 interaction0
	interaction1 interaction2 interaction3 interaction4 
	interaction5 interaction6 interaction7 interaction8 
	interaction9 interaction10 interaction11 interaction12
	interaction13 interaction14 interaction15";
	
	global interaction10 "interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	interaction_2 interaction_1 interaction0
	interaction1 interaction2 interaction3 interaction4 
	interaction5 interaction6 interaction7 interaction8 
	interaction9 interaction10";
	
	
	global rate1_interaction "interaction_15 interaction_14 interaction_13 interaction_12
	interaction_11 interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	 interaction_2 interaction_1 interaction1_0
	interaction1_1 interaction1_2 interaction1_3 interaction1_4 
	interaction1_5 interaction1_6 interaction1_7 interaction1_8 
	interaction1_9 interaction1_10 interaction1_11 interaction1_12
	interaction1_13 interaction1_14 interaction1_15";
	
	global rate2_interaction "interaction2_0 interaction2_1 interaction2_2 interaction2_3 interaction2_4 
	interaction2_5 interaction2_6 interaction2_7 interaction2_8 
	interaction2_9 interaction2_10 interaction2_11 interaction2_12
	interaction2_13 interaction2_14 interaction2_15";
	
	global rate3_interaction "interaction3_0 interaction3_1 interaction3_2 interaction3_3 interaction3_4 
	interaction3_5 interaction3_6 interaction3_7 interaction3_8 
	interaction3_9 interaction3_10 interaction3_11 interaction3_12
	interaction3_13 interaction3_14 interaction3_15";
	#d cr 
	
	
	eststo MatchedUtilEventStudyCov1: reghdfe logrealutilpersf $Cov1 rl_yr_rentpersf $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  MatchedUtilEventStudyCov1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo MatchedRentEventStudyCov1: reghdfe logrealrentpersf $Cov1 $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  MatchedRentEventStudyCov1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")

	
	*label variables for regression tables 	
	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	label var interaction_5 "5 Years Pre-Cert"
	label var interaction_4 "4 Years Pre-Cert"
	label var interaction_3 "3 Years Pre-Cert"
	label var interaction_2 "2 Years Pre-Cert"
	label var interaction_1 "1 Years Pre-Cert"
	label var interaction0 "Certification Year"
	label var interaction1 "1 Year Post-Cert"
	label var interaction2 "2 Year Post-Cert"
	label var interaction3 "3 Year Post-Cert"
	label var interaction4 "4 Year Post-Cert"
	label var interaction5 "5 Year Post-Cert"
	
	
	*make Latex Table 
	esttab MatchedRentEventStudyCov1 MatchedUtilEventStudyCov1 using "$results/13_MatchedEventStudy2_3dropped.tex" , label replace booktabs ///
	alignment(SS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	drop(interaction_10 interaction_9 interaction_8 ///
	interaction_7 interaction_6 interaction6 interaction7 interaction8 ///
	interaction9 interaction10) /// dropping the first and last 10 interaction terms to be able to fit table on one page
	title(Matched Sample: Energy Star Certification Event Study \label{tab13eventstudymatched3dropped}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	

*------------------------------------------------------------------------------*
*	Step 9- Matched Event Study	Robustness with 1 years before period dropped    *
*------------------------------------------------------------------------------*	

	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 



*Event Study 
	set more off 
	forvalues j=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`j'_wmatches.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
	
	*Generate Variable for Intensity of Treatment 
	bysort propnum: gen rate=everrated if year>=firstyearrated
	*Generate different bins for rating 	
	gen rate1=1 if rate>=75 & rate<82 
	replace rate1=0 if missing(rate1)
	gen rate2=1 if rate>=82 & rate<91 
	replace rate2=0 if missing(rate2)
	gen rate3=1 if rate>=91 & rate<100 
	replace rate3=0 if missing(rate3)
	*Generate a catogory for these so that I can plot them 
	gen rate_cat=. 
	replace rate_cat=1 if rate1==1 
	replace rate_cat=2 if rate2==1 
	replace rate_cat=3 if rate3==1 
	
	*Generate time since Treatment 
	gen years_pre_post_cert= year-firstyearrated
	*Generate indicators for 15 years pre and post cert (30 indicators)
	tostring years_pre_post_cert, replace
	replace years_pre_post_cert=subinstr(years_pre_post_cert, "-", "_", 1 )
	local years "_15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	foreach year of local years{
	generate pre`year'=1 if years_pre_post_cert=="`year'"
	}
	
	*Generate the interaction variable: 
	local years "_15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	foreach year of local years{
	generate interaction`year'=treat*pre`year'
	replace interaction`year'=0 if missing(interaction`year')
	label variable interaction`year' "`year'"
	}
	*Generate the interaction variable with the intensity var : 
	local years "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	local intensity "1 2 3" 
	foreach year of local years{
	foreach i of local intensity{
	generate interaction`i'_`year'=rate`i'*pre`year'
	replace interaction`i'_`year'=0 if missing(interaction`i'_`year')
	label variable interaction`i'_`year' "Rating Bin `i' `year'"
	}
	}	

	#d ;
	global interaction "interaction_15 interaction_14 interaction_13 interaction_12
	interaction_11 interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	interaction_2 interaction_1 interaction0
	interaction1 interaction2 interaction3 interaction4 
	interaction5 interaction6 interaction7 interaction8 
	interaction9 interaction10 interaction11 interaction12
	interaction13 interaction14 interaction15";
	
	global interaction10 "interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	interaction_3 interaction_2 interaction0
	interaction1 interaction2 interaction3 interaction4 
	interaction5 interaction6 interaction7 interaction8 
	interaction9 interaction10";
	
	
	global rate1_interaction "interaction_15 interaction_14 interaction_13 interaction_12
	interaction_11 interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	 interaction_2 interaction_1 interaction1_0
	interaction1_1 interaction1_2 interaction1_3 interaction1_4 
	interaction1_5 interaction1_6 interaction1_7 interaction1_8 
	interaction1_9 interaction1_10 interaction1_11 interaction1_12
	interaction1_13 interaction1_14 interaction1_15";
	
	global rate2_interaction "interaction2_0 interaction2_1 interaction2_2 interaction2_3 interaction2_4 
	interaction2_5 interaction2_6 interaction2_7 interaction2_8 
	interaction2_9 interaction2_10 interaction2_11 interaction2_12
	interaction2_13 interaction2_14 interaction2_15";
	
	global rate3_interaction "interaction3_0 interaction3_1 interaction3_2 interaction3_3 interaction3_4 
	interaction3_5 interaction3_6 interaction3_7 interaction3_8 
	interaction3_9 interaction3_10 interaction3_11 interaction3_12
	interaction3_13 interaction3_14 interaction3_15";
	#d cr 
	
	*Fe regression of util Exp with Heteroscedasticity and autocorrelation-consistent asymptotic variance (HAC)
	preserve
	eststo MatchedUtilEventStudyCov1: parmby "reghdfe logrealutilpersf $Cov1 rl_yr_rentpersf $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	*findit eclplot 
	*Plot the point estimates 
	keep if parmseq>=17 & parmseq<=30
	
	*Add row to plot the 0 point estimate 
	set obs `=_N+1'
	replace label = "_1" if label == "" 
	replace parmseq = 22.5 if parmseq == . 
	replace estimate = 0 if estimate == . 
	sort parmseq
	
	*Change the _ to - in the label variable 
	replace label=subinstr(label, "_", "-", 1 )
	sencode label if parm!="_cons", gene(parmlab)

	*Plot the outcomes 
	eclplot estimate min95 max95 parmlab, ytitle("Coefficient Estimates") ///
	xtitle("Treatment Effect By Years Before/After Certification") yline(0, lcolor(navy*.5)) ///
	xlabel(#15) bgcolor(white) graphregion(color(white)) ylab(,nogrid) xlab(,nogrid) legend(on ring(1) position(6) lab(1 "95% CI")) 
	gr export "$figures/19_util_eventstudy_matched`j'_1dropped.png", replace	
	restore
	
	preserve
	eststo MatchedUtilEventStudyCov2: parmby "reghdfe logrealutilpersf $Cov2 rl_yr_rentpersf $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	restore 
	preserve 
	eststo MatchedUtilEventStudyCov3: parmby "reghdfe logrealutilpersf $Cov3 rl_yr_rentpersf $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	restore
	
	*Fe regression with Heteroscedasticity and autocorrelation-consistent asymptotic variance (HAC)
	preserve
	eststo MatchedRentEventStudyCov1: parmby "reghdfe logrealrentpersf $Cov1 $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	*findit eclplot 
	*Plot the point estimates 
	keep if parmseq>=16 & parmseq<=29
	
	*Add row to plot the 0 point estimate 
	set obs `=_N+1'
	replace label = "_1" if label == "" 
	replace parmseq = 21.5 if parmseq == . 
	replace estimate = 0 if estimate == . 
	sort parmseq
	
	*Change the _ to - in the label variable 
	replace label=subinstr(label, "_", "-", 1 )
	sencode label if parm!="_cons", gene(parmlab)

	*Plot the outcomes 
	eclplot estimate min95 max95 parmlab, ytitle("Coefficient Estimates") ///
	xtitle("Treatment Effect By Years Before/After Certification") yline(0, lcolor(navy*.5)) ///
	xlabel(#15) bgcolor(white) graphregion(color(white)) ylab(,nogrid) xlab(,nogrid) legend(on ring(1) position(6) lab(1 "95% CI"))
	gr export "$figures/19_rent_eventstudy_matched`j'_1dropped.png", replace
	restore	
	
	preserve
	eststo MatchedRentEventStudyCov2: parmby "reghdfe logrealrentpersf $Cov2 $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	restore 
	preserve 
	eststo MatchedRentEventStudyCov3: parmby "reghdfe logrealrentpersf $Cov3 $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa) ", label norestore
	restore
	
	
	}
	
	
	**Repeat regressions to put output into latex table
		
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch2_wmatches.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	

	*Generate Variable for Intensity of Treatment 
	bysort propnum: gen rate=everrated if year>=firstyearrated
	*Generate different bins for rating 	
	gen rate1=1 if rate>=75 & rate<82 
	replace rate1=0 if missing(rate1)
	gen rate2=1 if rate>=82 & rate<91 
	replace rate2=0 if missing(rate2)
	gen rate3=1 if rate>=91 & rate<100 
	replace rate3=0 if missing(rate3)
	*Generate a catogory for these so that I can plot them 
	gen rate_cat=. 
	replace rate_cat=1 if rate1==1 
	replace rate_cat=2 if rate2==1 
	replace rate_cat=3 if rate3==1 
	
	*Generate time since Treatment 
	gen years_pre_post_cert= year-firstyearrated
	*Generate indicators for 15 years pre and post cert (30 indicators)
	tostring years_pre_post_cert, replace
	replace years_pre_post_cert=subinstr(years_pre_post_cert, "-", "_", 1 )
	local years "_15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	foreach year of local years{
	generate pre`year'=1 if years_pre_post_cert=="`year'"
	}
	
	*Generate the interaction variable: 
	local years "_15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	foreach year of local years{
	generate interaction`year'=treat*pre`year'
	replace interaction`year'=0 if missing(interaction`year')
	label variable interaction`year' "`year'"
	}
	*Generate the interaction variable with the intensity var : 
	local years "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
	local intensity "1 2 3" 
	foreach year of local years{
	foreach i of local intensity{
	generate interaction`i'_`year'=rate`i'*pre`year'
	replace interaction`i'_`year'=0 if missing(interaction`i'_`year')
	label variable interaction`i'_`year' "Rating Bin `i' `year'"
	}
	}	

	#d ;
	global interaction "interaction_15 interaction_14 interaction_13 interaction_12
	interaction_11 interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	interaction_2 interaction_1 interaction0
	interaction1 interaction2 interaction3 interaction4 
	interaction5 interaction6 interaction7 interaction8 
	interaction9 interaction10 interaction11 interaction12
	interaction13 interaction14 interaction15";
	
	global interaction10 "interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	 interaction_3 interaction_2 interaction0
	interaction1 interaction2 interaction3 interaction4 
	interaction5 interaction6 interaction7 interaction8 
	interaction9 interaction10";
	
	
	global rate1_interaction "interaction_15 interaction_14 interaction_13 interaction_12
	interaction_11 interaction_10 interaction_9 interaction_8
	interaction_7 interaction_6 interaction_5 interaction_4
	 interaction_2 interaction_1 interaction1_0
	interaction1_1 interaction1_2 interaction1_3 interaction1_4 
	interaction1_5 interaction1_6 interaction1_7 interaction1_8 
	interaction1_9 interaction1_10 interaction1_11 interaction1_12
	interaction1_13 interaction1_14 interaction1_15";
	
	global rate2_interaction "interaction2_0 interaction2_1 interaction2_2 interaction2_3 interaction2_4 
	interaction2_5 interaction2_6 interaction2_7 interaction2_8 
	interaction2_9 interaction2_10 interaction2_11 interaction2_12
	interaction2_13 interaction2_14 interaction2_15";
	
	global rate3_interaction "interaction3_0 interaction3_1 interaction3_2 interaction3_3 interaction3_4 
	interaction3_5 interaction3_6 interaction3_7 interaction3_8 
	interaction3_9 interaction3_10 interaction3_11 interaction3_12
	interaction3_13 interaction3_14 interaction3_15";
	#d cr 
	
	
	eststo MatchedUtilEventStudyCov1: reghdfe logrealutilpersf $Cov1 rl_yr_rentpersf $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  MatchedUtilEventStudyCov1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo MatchedRentEventStudyCov1: reghdfe logrealrentpersf $Cov1 $interaction10, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  MatchedRentEventStudyCov1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")

	
	*label variables for regression tables 	
	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	label var interaction_5 "5 Years Pre-Cert"
	label var interaction_4 "4 Years Pre-Cert"
	label var interaction_3 "3 Years Pre-Cert"
	label var interaction_2 "2 Years Pre-Cert"
	label var interaction_1 "1 Years Pre-Cert"
	label var interaction0 "Certification Year"
	label var interaction1 "1 Year Post-Cert"
	label var interaction2 "2 Year Post-Cert"
	label var interaction3 "3 Year Post-Cert"
	label var interaction4 "4 Year Post-Cert"
	label var interaction5 "5 Year Post-Cert"
	
	
	*make Latex Table 
	esttab MatchedRentEventStudyCov1 MatchedUtilEventStudyCov1 using "$results/13_MatchedEventStudy2_1dropped.tex" , label replace booktabs ///
	alignment(SS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	drop(interaction_10 interaction_9 interaction_8 ///
	interaction_7 interaction_6 interaction6 interaction7 interaction8 ///
	interaction9 interaction10) /// dropping the first and last 10 interaction terms to be able to fit table on one page
	title(Matched Sample: Energy Star Certification Event Study \label{13MatchedEventStudy21dropped}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})
